<?php

// [ 一键融资 ]
// `round`        char(20)      NOT NULL DEFAULT ''     COMMENT '融资轮次',
// `industry`     varchar(200)  NOT NULL DEFAULT ''     COMMENT '所属行业,JsonArray',
// `city`         varchar(40)   NOT NULL DEFAULT ''     COMMENT '所在城市',
// `money`        int(6)        NOT NULL DEFAULT 0      COMMENT '融资金额(万)',

// -- 会员(投资人)投资喜好表
// DROP TABLE IF EXISTS `jzl_member_like`;
// CREATE TABLE `jzl_member_like` (
// `member_id`    int(11)       NOT NULL DEFAULT 0      COMMENT '会员主键',
// `round`        varchar(400)  NOT NULL DEFAULT ''     COMMENT '投资轮次,JsonArray',
// `industry`     varchar(400)  NOT NULL DEFAULT ''     COMMENT '投资行业,JsonArray',
// `city`         varchar(400)  NOT NULL DEFAULT ''     COMMENT '关注城市,JsonArray',
// `begin_money`  int(6)        NOT NULL DEFAULT 0      COMMENT '投资启端金额(万)',
// `end_money`    int(6)        NOT NULL DEFAULT 0      COMMENT '投资末端金额(万)',
// PRIMARY KEY (`member_id`)
// ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='会员(投资人)投资喜好表';

// -- 项目推送表
// DROP TABLE IF EXISTS `jzl_project_push`;
// CREATE TABLE `jzl_project_push` (
// `project_id`       int(11)       NOT NULL DEFAULT 0  COMMENT '主键',
// `from_member_id`   int(11)       NOT NULL DEFAULT 0  COMMENT '推送者主键',
// `to_member_id`     int(11)       NOT NULL DEFAULT 0  COMMENT '接收者主键',
// `is_read`          tinyint(1)    NOT NULL DEFAULT 0  COMMENT '是否阅读',      
// `create_time`      int(10)       NOT NULL DEFAULT 0  COMMENT '推送时间',
// PRIMARY KEY (`project_id`),
// KEY (`from_member_id`),
// KEY (`to_member_id`),
// KEY (`create_time`)
// ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='项目推送表';

namespace app\common\logic;
use think\Db;

class KeyPush
{
    // 返回推送目标数量
    public static function run($project)
    {
        $count = 0;
        
        // 数据集分批处理
        $where['status']      = 1;
        $where['round']       = ['like','%' . $project['round'] . '%'];
        $where['industry']    = ['like','%' . $project['industry'] . '%'];
        $where['city']        = ['like','%' . $project['city'] . '%'];
        $where['begin_money'] = ['<=',$project['money']];
        $where['end_money']   = ['>=',$project['money']];
        Db::name('member_like')->where($where)->field('member_id')->chunk(100, function($list)use($project,&$count){

            if ( empty($list) ) return false;

            $data = [];
            foreach ($list as $key => $val) {

                // ------------------------------------------------------------------------------------------------------------------
                // 过滤重复投递
                $ret = Db::name('project_push')->where('project_id',$project['id'])->where('to_member_id',$val['member_id'])->find();
                if ( !is_null($ret) ) continue;
                // ------------------------------------------------------------------------------------------------------------------

                $data[$key]['project_id']             = $project['id'];

                // 投递人信息
                $fromMemberInfo = Db::name('member_info')->where('member_id',$project['member_id'])->find();
                $data[$key]['from_member_id']         = $project['member_id'];
                $data[$key]['from_mobile']            = $fromMemberInfo['mobile'];
                $data[$key]['from_member_nickname']   = $fromMemberInfo['nickname'];
                $data[$key]['from_member_headimgurl'] = $fromMemberInfo['headimgurl'];
                
                // 接收人信息
                $toMemberInfo = Db::name('member_info')->where('member_id',$val['member_id'])->find();
                $data[$key]['to_member_id']           = $val['member_id'];
                $data[$key]['to_mobile']              = $toMemberInfo['mobile'];
                $data[$key]['to_member_nickname']     = $toMemberInfo['nickname'];
                $data[$key]['to_member_headimgurl']   = $toMemberInfo['headimgurl'];
                
                $data[$key]['is_read']                = 0;
                $data[$key]['is_chat']                = 0;
                $data[$key]['create_time']            = time();
            }
            $num = Db::name('project_push')->insertAll($data);
            $count += $num;
        });

        return $count;
    }
}